package com.tjnu.baiyi.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.tjnu.baiyi.entity.TbAlbumVisitor;
import com.tjnu.baiyi.entity.TbUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * TbAlbumVisitor entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjnu.baiyi.dao.TbAlbumVisitor
 * @author MyEclipse Persistence Tools
 */
@Repository("tbAlbumVisitorDao")
@Transactional
public class TbAlbumVisitorDAO {
	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(TbAlbumVisitor transientInstance) {
		try {
			getCurrentSession().save(transientInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(TbAlbumVisitor persistentInstance) {
		try {
			getCurrentSession().delete(persistentInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TbAlbumVisitor findById(Integer id) {
		try {
			TbAlbumVisitor instance = (TbAlbumVisitor) getCurrentSession().get(
					TbAlbumVisitor.class, id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List<TbAlbumVisitor> findByAlbumId(Integer id) {
		try {
			String queryString = "FROM TbAlbumVisitor  WHERE tbAlbum.albumId=? ORDER BY albumVisitorTime DESC";
			Query queryObject = getCurrentSession().createQuery(queryString)
					.setInteger(0, id);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findAll() {
		try {
			String queryString = "from TbAlbumVisitor";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}